﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
// 日常练习
// 模拟实现各个内存操作函数

// 模拟实现memcpy
void* my_memcpy(void* dst, const void* src, size_t count)
{
    void* ret = dst;
    assert(dst);
    assert(src);
    /*
     * copy from lower addresses to higher addresses
     */
    while (count--) {
        *(char*)dst = *(char*)src;
        dst = (char*)dst + 1;
        src = (char*)src + 1;
    }

    return(ret);
}

// 模拟实现memmove
void* my_memmove(void* dst, const void* src, size_t count)
{
         void* ret = dst; ​
        if (dst <= src || (char*)dst >= ((char*)src + count)) {
            /*
            * Non-Overlapping Buffers
            * copy from lower addresses to higher addresses
            */
            while (count--) {
                *(char*)dst = *(char*)src;
                dst = (char*)dst + 1;
                src = (char*)src + 1;
            }
        }
        else {
            /*
            * Overlapping Buffers
            * copy from higher addresses to lower addresses
            */
            dst = (char*)dst + count - 1;
            src = (char*)src + count - 1;
            ​
                while (count--) {
                    *(char*)dst = *(char*)src;
                    dst = (char*)dst - 1;
                    src = (char*)src - 1;
                }
        }
    ​
        return(ret);
}

int main() {
	
	return 0;
}